/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBPengadaan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Pengadaan;

/**
 *
 *
 * @author elfatah
 */
public class PengadaanServlet extends HttpServlet {

    private int  barang, jmlbarang, hargaSatuan;
    private String pegawai, supplier;
    String action, id;
    Pengadaan bean = new Pengadaan();
    DBPengadaan dbobject = new DBPengadaan();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        action = request.getParameter("action");
        id = request.getParameter("id");
        if (action.equals("edit")) {
            try {
                //get userdetails of particular name
                Pengadaan ubean = dbobject.getPengadaanDetails(id);
                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("insert_pengadaan");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("delete")) {
            try {
                //delete userdetails of particular name
                dbobject.deletePengadaan(id);
//                List list = dbobject.getAlldetails();
//                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("view_pengadaan");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        pegawai = request.getParameter("pegawai");
        supplier = request.getParameter("supplier");
        //barang = Integer.parseInt(request.getParameter("barang"));
        jmlbarang = Integer.parseInt(request.getParameter("jmlbarang"));
        hargaSatuan = Integer.parseInt(request.getParameter("hargaSatuan"));
        action = request.getParameter("action");
        id = request.getParameter("id");

        if (action.equals("Insert")) {
            try {

                setValuesToBean();
                dbobject.insertPengadaan(bean);
                out.print("<script>window.alert('Penambahan pengadaan sukses');window.location=('insert_pengadaan')</script>");

//                RequestDispatcher rd = request.getRequestDispatcher("insert_pengadaan.jsp");
//                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PengadaanServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PengadaanServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("Update")) {
            try {
                setValuesToBean();

                //using DBClass object call insertDetails method and pass bean object
                dbobject.UpdatePengadaan(bean, id);
              
                //forward to insertupdate page using requestdispatcher
                out.print("<script>window.alert('Perubahan data pengadaan sukses');window.location=('view_pengadaan')</script>");

//                RequestDispatcher rd = request.getRequestDispatcher("view_pengadaan.jsp");
//                //display a message to client.store message in request object
//                //forwarding to jsp
//                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void setValuesToBean() {
        bean.setNoPegawai(pegawai);
        bean.setNoSupplier(supplier);
       // bean.setIdBarang(barang);
        bean.setJumlahBarang(jmlbarang);
        bean.setHargaSatuan(hargaSatuan);

    }
}
